//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package user_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/user_resume_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type BasicInfoDbItem struct {
	TrueName    string `bson:"trueName"`
	Gender      int32  `bson:"gender"`
	Birthday    int64  `bson:"birthday"`
	HasBirthDay bool   `bson:"hasBirthDay"`
	MobilePhone string `bson:"mobilePhone"`
	Email       string `bson:"email"`
	SelfIntro   string `bson:"selfIntro"`
}

type WorkExpDbItem struct {
	FromTime    int64  `bson:"fromTime"`
	HasFromTime bool   `bson:"hasFromTime"`
	ToTime      int64  `bson:"toTime"`
	HasToTime   bool   `bson:"hasToTime"`
	Company     string `bson:"company"`
	Positon     string `bson:"positon"`
	WorkDesc    string `bson:"workDesc"`
}

type EduExpDbItem struct {
	FromTime    int64  `bson:"fromTime"`
	HasFromTime bool   `bson:"hasFromTime"`
	ToTime      int64  `bson:"toTime"`
	HasToTime   bool   `bson:"hasToTime"`
	SchoolName  string `bson:"schoolName"`
	MajorName   string `bson:"majorName"`
}

type UserResumeInfoDbItem struct {
	UserId          string          `bson:"_id"`
	BasicInfo       BasicInfoDbItem `bson:"basicInfo"`
	WorkExpItemList []WorkExpDbItem `bson:"workExpItemList"`
	EduExpItemList  []EduExpDbItem  `bson:"dduExpItemList"`
}

func (item *UserResumeInfoDbItem) ToUserResumeInfo(accessItem *ResumeAccessDbItem) *user_resume_api.UserResumeInfo {
	basicInfo := &user_resume_api.BasicInfo{
		TrueName:    item.BasicInfo.TrueName,
		Gender:      user_resume_api.GENDER_TYPE(item.BasicInfo.Gender),
		Birthday:    item.BasicInfo.Birthday,
		HasBirthDay: item.BasicInfo.HasBirthDay,
		MobilePhone: item.BasicInfo.MobilePhone,
		Email:       item.BasicInfo.Email,
		SelfIntro:   item.BasicInfo.SelfIntro,
	}
	workExpList := []*user_resume_api.WorkExpItem{}
	for _, workExpItem := range item.WorkExpItemList {
		workExpList = append(workExpList, &user_resume_api.WorkExpItem{
			FromTime:    workExpItem.FromTime,
			HasFromTime: workExpItem.HasFromTime,
			ToTime:      workExpItem.ToTime,
			HasToTime:   workExpItem.HasToTime,
			Company:     workExpItem.Company,
			Positon:     workExpItem.Positon,
			WorkDesc:    workExpItem.WorkDesc,
		})
	}
	eduExpList := []*user_resume_api.EduExpItem{}
	for _, eduExpItem := range item.EduExpItemList {
		eduExpList = append(eduExpList, &user_resume_api.EduExpItem{
			FromTime:    eduExpItem.FromTime,
			HasFromTime: eduExpItem.HasFromTime,
			ToTime:      eduExpItem.ToTime,
			HasToTime:   eduExpItem.HasToTime,
			SchoolName:  eduExpItem.SchoolName,
			MajorName:   eduExpItem.MajorName,
		})
	}
	return &user_resume_api.UserResumeInfo{
		BasicInfo:       basicInfo,
		WorkExpItemList: workExpList,
		EduExpItemList:  eduExpList,

		AllowProjectAccess: accessItem.AllowProjectAccess,
		AllowOrgAccess:     accessItem.AllowOrgAccess,
	}
}

type _UserResumeDao struct {
	mc *mongo.Collection
}

func newUserResumeDao(db *mongo.Database) (*_UserResumeDao, error) {
	mc := db.Collection(db_ns.DB_NS_USER + "resume")
	return &_UserResumeDao{mc: mc}, nil
}

func (db *_UserResumeDao) Set(ctx context.Context, dbItem *UserResumeInfoDbItem) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": dbItem.UserId}, bson.M{"$set": dbItem}, options.Update().SetUpsert(true))
	return err
}

func (db *_UserResumeDao) Get(ctx context.Context, userId string) (*UserResumeInfoDbItem, error) {
	retItem := &UserResumeInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": userId}).Decode(retItem)
	return retItem, err
}
